using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class DataImport_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnImportVenues_Click(object sender, EventArgs e)
    {
        //Get all of the venues in tblAgencies
        PlacementManager.tblAgencyComponent otblAgencyC = new PlacementManager.tblAgencyComponent();
        List<PlacementManager.tblAgency> oAgencyList = otblAgencyC.SelectAll("AgencyName");


        PlacementManager.VenueComponent oVenueC = new PlacementManager.VenueComponent();
        int newVenueId = 0;


        foreach (PlacementManager.tblAgency oAgency in oAgencyList)
        {
            Response.Write("<br />Agency NAme: " + oAgency.AgencyName);

            //Check if it exists in the Venues table

            if (oAgency.AgencyName.Length > 0)
            {
                PlacementManager.Venue oVenue = oVenueC.SelectOneWVenueName(oAgency.AgencyName);
                if (oVenue.VenueId > 0)//it exists already
                {

                }
                else // it doesn't exist
                {
                    //so add it
                    oVenue.CreatedBy = User.Identity.Name;
                    oVenue.DateCreated = DateTime.Now;
                    oVenue.DateUpdated = DateTime.Now;
                    if (System.Convert.IsDBNull(oAgency.Fax) || oAgency.Fax.Length == 0)
                    {
                        oVenue.Fax = string.Empty;
                    }
                    else
                    {
                        oVenue.Fax = oAgency.Fax;
                    }

                    if (System.Convert.IsDBNull(oAgency.Email) || oAgency.Email.Length == 0)
                    {
                        oVenue.GenericEmail = string.Empty;
                    }
                    else
                    {
                        oVenue.GenericEmail = oAgency.Email;
                    }

                    oVenue.IsActive = true;

                    if (System.Convert.IsDBNull(oAgency.Phone) || oAgency.Phone.Length == 0)
                    {
                        oVenue.TelephoneNumber = string.Empty;
                    }
                    else
                    {
                        oVenue.TelephoneNumber = oAgency.Phone;
                    }

                    oVenue.UpdatedBy = User.Identity.Name;

                    if (System.Convert.IsDBNull(oAgency.AgencyName) || oAgency.AgencyName.Length == 0)
                    {
                        oVenue.VenueName = string.Empty;
                    }
                    else
                    {
                        oVenue.VenueName = oAgency.AgencyName;
                    }

                    newVenueId = oVenueC.Insert(oVenue);

                    int newAddressId = this.AddAddress(newVenueId, oAgency);
                    int newPostalAddressId = this.AddPostalAddress(newVenueId, oAgency);

                    if (newAddressId > 0)
                    {
                        this.AddAddressVenueLink(newVenueId, newAddressId, true);
                    }

                    if (newPostalAddressId > 0)
                    {
                        this.AddAddressVenueLink(newVenueId, newPostalAddressId, false);
                    }
                }
            }
        }
    }
  
    
    protected int AddAddress(int venueId, PlacementManager.tblAgency oAgency)
    {
        //Now we don't want to add the same address twice so we'd better check if it exists
        PlacementManager.AddressComponent oAddressC = new PlacementManager.AddressComponent();
        PlacementManager.Address oAddress = oAddressC.SelectOneWAddress1(oAgency.StreetAddress);
       
        int addressId = 0;

        if (oAddress.AddressId == 0) 
        {
            oAddress.Address1 = oAgency.StreetAddress;
            oAddress.Address2 = string.Empty;
            oAddress.City = oAgency.TownCode;
            oAddress.CountryId = 14;
            oAddress.CreatedBy = User.Identity.Name;
            oAddress.DateCreated = DateTime.Now;
            oAddress.DateUpdated = DateTime.Now;
            oAddress.IsActive = true;
            oAddress.IsPostal = false;
            oAddress.PostCode = this.GetPostCode(oAgency.TownCode);
            oAddress.StateId = this.GetState(oAgency.TownCode);
            oAddress.UpdatedBy = User.Identity.Name;
            addressId = oAddressC.Insert(oAddress); 
        }

        return addressId;
    }

    protected int AddPostalAddress(int venueId, PlacementManager.tblAgency oAgency)
    {
        //Now we don't want to add the same address twice so we'd better check if it exists
        PlacementManager.AddressComponent oAddressC = new PlacementManager.AddressComponent();
        PlacementManager.Address oAddressPostal = oAddressC.SelectOneWAddress1(oAgency.PostalAddress);

        int addressId = 0;

        if (oAddressPostal.AddressId == 0) 
        {
            oAddressPostal.Address1 = oAgency.PostalAddress;
            oAddressPostal.Address2 = string.Empty;
            oAddressPostal.City = oAgency.TownCode;
            oAddressPostal.CountryId = 14;
            oAddressPostal.CreatedBy = User.Identity.Name;
            oAddressPostal.DateCreated = DateTime.Now;
            oAddressPostal.DateUpdated = DateTime.Now;
            oAddressPostal.IsActive = true;
            oAddressPostal.IsPostal = false;
            oAddressPostal.PostCode = this.GetPostCode(oAgency.TownCode);
            oAddressPostal.StateId = this.GetState(oAgency.TownCode);
            oAddressPostal.UpdatedBy = User.Identity.Name;
            addressId = oAddressC.Insert(oAddressPostal);
        }

        return addressId;
    }

    protected string GetPostCode(string TownCode)
    {
        PlacementManager.Geo_PostcodesComponent oPostCodesC = new PlacementManager.Geo_PostcodesComponent();
        List<PlacementManager.Geo_Postcodes> oPostCodesList = oPostCodesC.SelectAll("TownCode");

        string poscodeToReturn = string.Empty;

        foreach (PlacementManager.Geo_Postcodes oPC in oPostCodesList)
        {
            if (oPC.TownCode == TownCode)
            {
                poscodeToReturn = oPC.Postcode.ToString();
                break;
            }
        }

        return poscodeToReturn;
    }

    protected int GetState(string TownCode)
    {
        PlacementManager.Geo_PostcodesComponent oPostCodesC = new PlacementManager.Geo_PostcodesComponent();
        List<PlacementManager.Geo_Postcodes> oPostCodesList = oPostCodesC.SelectAll("TownCode");

        int stateId = 2;
        string stateCode = "VIC";

        foreach (PlacementManager.Geo_Postcodes oPC in oPostCodesList)
        {
            if (oPC.TownCode == TownCode)
            {
                stateCode = oPC.State;
                break;
            }
        }

        switch (stateCode)
        {
            case "VIC":
                stateId = 2;
                break;
            case "TAS":
                stateId = 1;
                break;
            case "NSW":
                stateId = 3;
                break;
            case "QLD":
                stateId = 4;
                break;
            case "SA":
                stateId = 5;
                break;
            case "WA":
                stateId = 6;
                break;
            case "NT":
                stateId = 7;
                break;
            case "ACT":
                stateId = 8;
                break;
            default:
                stateId = 2;
                break;
        }

        return stateId;

    }

    protected int AddAddressVenueLink(int newVenueId, int newAddressId, bool isPrimary)
    {
        PlacementManager.LinkVenueAddressComponent oLvaC = new PlacementManager.LinkVenueAddressComponent();
        PlacementManager.LinkVenueAddress oLVA = new PlacementManager.LinkVenueAddress();
        oLVA.AddressId = newAddressId;
        oLVA.CreatedBy = User.Identity.Name;
        oLVA.DateCreated = DateTime.Now;
        oLVA.DateUpdated = DateTime.Now;
        oLVA.IsActive = true;
        oLVA.IsPrimary = isPrimary;
        oLVA.UpdatedBy = User.Identity.Name;
        oLVA.VenueId = newVenueId;
        return oLvaC.Insert(oLVA);
    }
    protected void btnUpdatePostCodes_Click(object sender, EventArgs e)
    {
        PlacementManager.AddressComponent oAddressC = new PlacementManager.AddressComponent();
        List<PlacementManager.Address> oAddressList = oAddressC.SelectAll("VenueName");

        PlacementManager.Geo_PostcodesComponent oPostCodeC = new PlacementManager.Geo_PostcodesComponent();
 
        
        foreach (PlacementManager.Address oAddress in oAddressList)
        {
            //get the postcode object
            PlacementManager.Geo_Postcodes oPostCode = oPostCodeC.SelectOneWTownCode(oAddress.City);
            oAddress.PostCode = oPostCode.Postcode.ToString();
            oAddressC.Update(oAddress);
        }
    }
}
